package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import paintball.PaintballPlayer;

/**
 * Server class, acts as a listener for connections.
 * @author Twisty
 *
 */
public class Server implements Runnable {
	// Thread the server runs in.
	private Thread t;
	
	// Server socket for listening.
	private ServerSocket sock;

	/**
	 * Class constructor.
	 */
	public Server() {
		this.t = new Thread(this);
	}
	
	/**
	 * Start this thread.
	 */
	public void start() {
		this.t.start();
	}

	@Override
	public void run() {
		try {
			sock = new ServerSocket(1234);
			for (;;) {
				Socket newsock = sock.accept();

				System.out.println("Accepting new player: ");

				new PaintballPlayer(newsock);
			}
		} catch (Exception e) {
			System.out.println("IO error " + e);
		}
		System.out.println("End!");
		
		try {
			sock.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}